<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\DbConnection\Db;
/**
 * @Controller()
 */
class CategoryController extends AbstractController
{
    /**
     * @RequestMapping(path="getlist", method="get")
     */
    public function getlist()
    {
        return [
            'status' => 1,
            'list'=> $this->get_category_children()
        ];
    }
    public function get_category_children($parent_id=0){
        $row = Db::table('category')->where('parent_id', $parent_id);
        if($row->exists()){
            $category=$row->get();
            $categories=[];
            foreach ($category as $item) {
                array_push($categories,['value'=>$item->id,'label'=>$item->name,'children'=>get_category_children($item->id)]);
            }
            return $categories;
        }
        else{
            return [];
        }
    }
}
